package com.hst.capacity.infra.persistence.jpa.vehicle;

import com.hst.capacity.domain.model.entity.vehicle.VehicleEntity;
import com.hst.capacity.domain.model.response.LabelValueVO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface VehicleRepository extends JpaRepository<VehicleEntity, String>, JpaSpecificationExecutor<VehicleEntity> {

    /**
     * 根据车牌信息查找车辆信息
     * @param plateNumber
     * @return
     */
    VehicleEntity findByPlateNumber(String plateNumber);

    /**
     * 根据车牌信息查找车辆信息
     * @param plateNumber
     * @return
     */
    VehicleEntity findAllByPlateNumber(String plateNumber);

    /**
     * 查询被司机绑定的车辆信息
     * @return
     */
    @Query("select v " +
            "from DriverEntity d " +
            "inner join VehicleEntity v on d.idVehicle = v.idVehicle " +
            "inner join VehicleTypeEntity vt on v.idVehicleType = vt.idVehicleType " +
            "where d.idDriver=:idDriver")
    VehicleEntity findByIdDriver(@Param("idDriver") String idDriver);

    /**
     * 根据ID车辆信息
     * @param idVehicle
     * @return
     */
    VehicleEntity findByIdVehicle(String idVehicle);

    @Query("select new com.hst.capacity.domain.model.response.LabelValueVO(v.plateNumber) from VehicleEntity v where v.idFranchisee=:idf and v.plateNumber like CONCAT('%',:str,'%')")
    List<LabelValueVO> findByIdFranchiseeAndPlateNumberLike(String idf, String str);

}
